package com.kk.base.util;

import java.lang.reflect.Field;
import java.util.regex.Pattern;

import com.kk.base.annotion.ValidateEntity;
import com.kk.base.bean.CodeMsg;
import org.apache.commons.lang3.StringUtils;

/**
 * 验证实体工具类
 *
 * @author Administrator
 */
public class ValidateEntityUtil {

    public static CodeMsg validate(Object object) {
        Field[] declaredFields = object.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            ValidateEntity annotation = field.getAnnotation(ValidateEntity.class);
            if (annotation != null) {
                if (annotation.required()) {
                    //表示该字段是必填字段
                    field.setAccessible(true);
                    try {
                        Object o = field.get(object);
                        //首先判断是否为空
                        if (o == null) {
                            CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                            codeMsg.setMsg(annotation.errorRequiredMsg());
                            return codeMsg;
                        }
                        //到这，说明该变量的值不是null
                        //首先判断是不是String类型
                        if (o instanceof String) {
                            //先判断是否等于空字符串
                            if ("".equals(o.toString())) {
                                CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                codeMsg.setMsg(annotation.errorRequiredMsg());
                                return codeMsg;
                            }
                            //若是字符串类型，则检查其长度
                            if (annotation.requiredLength()) {
                                if (o.toString().length() < annotation.minLength()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMinLengthMsg());
                                    return codeMsg;
                                }
                                if (o.toString().length() > annotation.maxLength()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMaxLengthMsg());
                                    return codeMsg;
                                }
                            }
                        }
                        //其次来判断是否为数字
                        if (isNumberObject(o)) {
                            //判断是否规定检查最小值
                            if (annotation.requiredMinValue()) {
                                if (Double.parseDouble(o.toString()) < annotation.minValue()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMinValueMsg());
                                    return codeMsg;
                                }
                            }
                            //判断是否规定检查最大值
                            if (annotation.requiredMaxValue()) {
                                if (Double.parseDouble(o.toString()) > annotation.maxValue()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMaxValueMsg());
                                    return codeMsg;
                                }
                            }
                        }
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return CodeMsg.SUCCESS;
    }

    /**
     * 检查对象是否是数字类型
     */
    public static boolean isNumberObject(Object object) {
        return object instanceof Number;
    }


    private static final String REGEX_MOBILE = "((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19(1|[8-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))";

    /**
     * 判断是否是手机号
     *
     * @param tel 手机号
     * @return boolean true:是  false:否
     */
    public static boolean isMobile(String tel) {
        if (StringUtils.isEmpty(tel)) {
            return false;
        }
        return Pattern.matches(REGEX_MOBILE, tel);
    }
}
